﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Resources;

namespace Tomahawk.Runtime.Canvas.Sprite
{
    /// <summary>
    /// An UISprite is the tipical Sprite used for user interfaces. 
    /// User interface textures usually don't share the same texture, 
    /// and should be drawed in inmediate mode for best performance
    /// </summary>
    public class UISprite : Sprite
    {
        /// <summary>
        /// Holds the texture asset path
        /// </summary>
        private string textureAsset;

        /// <summary>
        /// Constructor
        /// </summary>
        public UISprite(string textureAsset, string effectChannel, ResourceManager.ResourcePool pool)
        {
            this.resourcePool = pool;
            this.textureAsset = textureAsset;
            this.EffectChannel = effectChannel;
        }

        /// <summary>
        /// Texture this sprite will be using
        /// </summary>
        Texture2D texture;

        /// <summary>
        /// Height of the original texture.
        /// </summary>
        public float OriginalHeigth
        {
            get { return texture.Height; }
        }

        /// <summary>
        /// Width of the original texture.
        /// </summary>
        public float OriginalWidth
        {
            get { return texture.Width; }
        }

        private Vector2 origin = new Vector2(0.0f, 0.0f);
        public Vector2 Origin { get { return origin; } set { origin = value; } }

        private float rotation = 0.0f;
        public float Rotation { get { return rotation; } set { rotation = value; } }

        private Rectangle _screenRectangle = new Rectangle();

        /// <summary>
        /// Draw the primitive, must be called with an already opened and configured SpriteBatch
        /// </summary>
        public override void Render(SpriteBatch spriteBatch)
        {
            if (!this.texture.IsDisposed)
            {
                Engine.Instance.CanvasManager.ApplyVirtualCanvas(ref this.DestinationRectangle, ref _screenRectangle);
                spriteBatch.Draw(this.texture, _screenRectangle, this.SourceRectangle, this.Color,
                    rotation, origin, this.Effect, this.Depth);
            }
        }

        /// <summary>
        /// Load the contents
        /// </summary>
        protected override bool OnLoad(GraphicsDevice graphics, ContentManager contentManager)
        {
            //load the texture
            this.texture = contentManager.Load<Texture2D>(this.textureAsset);

            //set default source size
            this.SourceRectangle = new Microsoft.Xna.Framework.Rectangle(0, 0, this.texture.Width, this.texture.Height);

            //
            return true;
        }

    }

}
